package com.elite.groovy.callback

import cn.com.kh.HttpClientProxy
import cn.com.tpUtil.GetXMLValue
import com.elite.groovy.util.EliteBean
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
/*public class PolicyStatusSynchronization implements ServiceBackInvoke{*/
public class PolicyStatusSynchronization implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(PolicyStatusSynchronization.class);
	/*public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response){ */
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
		
	    //查询需要自动同步的保单号
		log.debug("----------------开始执行保单状态回写Grovvy----------------");
		def selectSQL = "select a.offer_no from (select t.quote_no offer_no, t.payduedate payduedate from tabccarinsurance t where 1 = 1 and t.quote_no is not null and (t.full_policy_no is null or t.sy_policycode is null) and t.payduedate > sysdate - 1 and t.riskcode in  (select risk_code from c_riskcode where risk_type = 1)  union all  select t.offerno, t.payduedate   from tabccarinsurance t where 1 = 1 and t.offerno is not null and (t.full_policy_no is null or t.jq_policycode is null)  and t.payduedate > sysdate - 1   and t.riskcode in  (select risk_code from c_riskcode where risk_type = 1)) a where 1 = 1 and rownum < 500 order by a.payduedate desc";		
		def resultList = eliteBean.chaxun_sql(selectSQL,"dataSource");
		def resultListNum = resultList.size();
		
		log.debug("本次调用数量PolicyStatus:"+resultListNum);
		def selectSQL2="SELECT WEBSERVICEURL FROM SYSC_INTERFACE where  WEBTYPE='MVURL'";
		def postURL2=eliteBean.chaxun_sql(selectSQL2,"dataSource");
		def postURL=postURL2[0].get("WEBSERVICEURL");

		def OFFERNO="";
		
		if(resultListNum>0) {									
			def postXml="";
			def result="";
			def PROPOSAL="";
			for (int i=0;i<resultListNum;i++) {	
			    def queryMap1=resultList[i];
			    OFFERNO=queryMap1.get("offer_no");
				
				postXml="<?xml version='1.0' encoding='UTF-8'?><PACKET TYPE='REQUEST' VERSION='1.0'><HEAD><REQUEST_TYPE>D17</REQUEST_TYPE></HEAD><BODY><BASE_PART><PROPOSAL_NO>"+OFFERNO+"</PROPOSAL_NO></BASE_PART></BODY></PACKET>";	

				result =  HttpClientProxy.sendPost(postURL,postXml,"",8888);
				//log.debug("调用接口结果:"+result);
				String responseType = GetXMLValue.main(result,"REQUEST_TYPE");
				String responseCode = GetXMLValue.main(result,"RESPONSE_CODE");
				String responseErrorMsg = GetXMLValue.main(result,"ERROR_MESSAGE");
				
					//log.debug("调用接口类型:"+responseType);
					//log.debug("调用成功标识1/0:"+responseCode);
					//log.debug("调用接口失败返回:"+responseErrorMsg);
				if(responseCode=="1") {
					//log.debug("获取保单状态接口调用成功");
					String PROPOSALNO = GetXMLValue.readXml(result,"BASE_PART","PROPOSAL_NO");
					String RISKCODE = GetXMLValue.readXml(result,"BASE_PART","RISK_CODE");
					String POLICYNO= GetXMLValue.readXml(result,"BASE_PART","POLICY_NO");
					String FORCENO= GetXMLValue.readXml(result,"BASE_PART","FORCE_NO");
					String BUSINESSNO= GetXMLValue.readXml(result,"BASE_PART","BUSINESS_NO");
					String UNDERWRITEIND= GetXMLValue.readXml(result,"BASE_PART","UNDERWRITE_IND");
					String UNDERWRITEENDDATE= GetXMLValue.readXml(result,"BASE_PART","UNDERWRITE_ENDDATE");
					String ACCEPTDATE= GetXMLValue.readXml(result,"BASE_PART","ACCEPT_DATE");
					String PLANFEE= GetXMLValue.readXml(result,"BASE_PART","PLAN_FEE");
					String RECEIVEIND= GetXMLValue.readXml(result,"BASE_PART","RECEIVE_IND");
					String RECEIVEFEE= GetXMLValue.readXml(result,"BASE_PART","RECEIVE_FEE");
					String RECEIVEDATE= GetXMLValue.readXml(result,"BASE_PART","RECEIVE_DATE");
				
					//log.debug("大保单号"+POLICYNO);
					//log.debug("交强险保单号"+FORCENO);
					//log.debug("商业险保单号"+BUSINESSNO);
					if(POLICYNO==null||POLICYNO==""){
						if(BUSINESSNO==null||BUSINESSNO==""){
							PROPOSAL=FORCENO;
							//log.debug("大保单号等于交强险保单号："+PROPOSAL);
						}else
						{
							PROPOSAL=BUSINESSNO;
							//log.debug("大保单号等于商业险保单号："+PROPOSAL);
						}
					}
					else
					{
						PROPOSAL=POLICYNO;
						//log.debug("大保单号"+PROPOSAL);
					}
					//log.debug("险种信息"+RISKCODE);
					//log.debug("交强险保单号"+FORCENO);
					//log.debug("商业险保单号"+BUSINESSNO);
					//log.debug("保单状态"+UNDERWRITEIND);

					if(UNDERWRITEENDDATE==null||UNDERWRITEENDDATE=="")
					{
						UNDERWRITEENDDATE="";
						//log.debug("核保通过时间"+UNDERWRITEENDDATE);
					}
					if(ACCEPTDATE==null||ACCEPTDATE=="")
					{
						ACCEPTDATE="";
						//log.debug("支付时间"+ACCEPTDATE);
					}
					if(UNDERWRITEIND=='6')
					{
					    if(FORCENO==null||FORCENO==""){
						def updateSQL1="update tabccarinsurance t set  t.full_policy_no='"+PROPOSAL+"',t.sy_policycode='"+BUSINESSNO+"',t.insurance_status= '"+UNDERWRITEIND+"',t.checkdate=to_date('"+UNDERWRITEENDDATE+"','yyyy-mm-dd hh24:mi:ss'),t.paydate=to_date('"+ACCEPTDATE+"','yyyy-mm-dd hh24:mi:ss') where (t.quote_no ='"+OFFERNO+"' or t.offerno='"+OFFERNO+"')";
						//log.debug("打印更新tabccarinsurance:"+updateSQL1);
						eliteBean.update(updateSQL1,'dataSource');
						}
						else{
						def updateSQL1="update tabccarinsurance t set  t.full_policy_no='"+PROPOSAL+"',t.jq_policycode='"+FORCENO+"',t.insurance_status= '"+UNDERWRITEIND+"',t.checkdate=to_date('"+UNDERWRITEENDDATE+"','yyyy-mm-dd hh24:mi:ss'),t.paydate=to_date('"+ACCEPTDATE+"','yyyy-mm-dd hh24:mi:ss') where (t.quote_no ='"+OFFERNO+"' or t.offerno='"+OFFERNO+"')";
						//log.debug("打印更新tabccarinsurance:"+updateSQL1);
						eliteBean.update(updateSQL1,'dataSource');
						}
						def updateSQL2="update tabcrelation t set t.policy_no='"+PROPOSAL+"',t.check_date =to_date('"+UNDERWRITEENDDATE+"','yyyy-mm-dd hh24:mi:ss'),t.paydate=to_date('"+ACCEPTDATE+"','yyyy-mm-dd hh24:mi:ss') where (t.quote_no ='"+OFFERNO+"' or offer_no='"+OFFERNO+"')";
						//log.debug("更新成功tabcrelation:"+updateSQL2);
						eliteBean.update(updateSQL2,'dataSource');
					}	
						def insertSQL = "insert into PolicyStatusSynchronization_log(REQUEST_TYPE,createddate,PROPOSAL_NO,ERROR_MESSAGE,grovvy,back)select '"+responseType+"',sysdate,'"+OFFERNO+"','"+responseErrorMsg+"','PolicyStatusSynchronization.Grovvy','"+result+"' from dual";
						eliteBean.update(insertSQL, 'dataSource');
						//log.debug("GROVVY调用成功添加日志:" + insertSQL);
					
				}
					else{
					def insertSQL = "insert into PolicyStatusSynchronization_log(REQUEST_TYPE,createddate,PROPOSAL_NO,ERROR_MESSAGE,grovvy,back)select '"+responseType+"',sysdate,'"+OFFERNO+"','"+responseErrorMsg+"','PolicyStatusSynchronization.Grovvy','"+result+"' from dual";
					eliteBean.update(insertSQL, 'dataSource');
					log.debug("GROVVY调用失败添加日志:" + insertSQL);
					}
					
				}
					log.debug("更新成功请查询PolicyStatusSynchronization_log");
			}
		else{
				log.debug("当前未有数据不执行调用");
			}			
	}
}